package com.greatwideweb.sendtoself;

import java.util.ArrayList;
import java.util.regex.Pattern;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.Activity;
import android.content.Intent;

import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.util.Patterns;
import android.widget.EditText;
import android.widget.LinearLayout;


public class SendToSelfActivity extends Activity{
  
	String email;
  LinearLayout layout;
  EditText txt;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        email = fetchConfiguredEmail();
        //Log.d("SendToSelf", "onCreate");
    }
    
    private String fetchConfiguredEmail() {
	    // TODO Auto-generated method stub
	    return null;
    }

		@Override
    protected void onResume() {
      // TODO Auto-generated method stub
      super.onResume();
      //Log.d("SendToSelf", "onResume");
      Intent intent = getIntent();
      //Log.d("test3", intent.getAction() + " test4: " + intent.getDataString() );
      //Log.d("SendToSelf", "Extra: " + intent.getStringExtra(Intent.EXTRA_TEXT ));
      
      if(email == null)
      	 email = fetchConfiguredEmail();
      
      if(email == null) 
      	email = getAccountEmail();
      
      
      //Log.d("SendToSelf", "emailAddress after: " + email);
      
      String link = intent.getStringExtra(Intent.EXTRA_TEXT );
      //Intent sendIntent = new Intent(Intent.ACTION_SEND);
      Intent sendIntent = new Intent(Intent.ACTION_VIEW);
      sendIntent.setClassName("com.google.android.gm", "com.google.android.gm.ComposeActivityGmail");
      /*
      ArrayList<String> classes = getClasses();
      if(classes != null && classes.size() > 1) {
      	sendIntent.setClassName(classes.get(0), classes.get(1));
      }
      */
      sendIntent.putExtra(Intent.EXTRA_EMAIL, new String[] {email });
      sendIntent.setData(Uri.parse(email));
      sendIntent.putExtra(Intent.EXTRA_SUBJECT, "Send to Self");
      sendIntent.setType("plain/text");
      sendIntent.putExtra(Intent.EXTRA_TEXT, link);
      startActivity(sendIntent);
      finish();
      
    }
    
    private ArrayList<String> getClasses() {
	    // TODO Auto-generated method stub
    	ArrayList<String> result = new ArrayList<String>();
    	result.add("com.google.android.gm");
    	result.add("com.google.android.gm.ComposeActivityGmail");
	    return null;
    }

		private String getAccountEmail() {
    	String result=null;
    	Pattern emailPattern = Patterns.EMAIL_ADDRESS; // API level 8+
      Account[] accounts = AccountManager.get(this).getAccounts();
    	for (Account account : accounts) {
        if (emailPattern.matcher(account.name).matches()) {
        	result = account.name;
        	if(result.contains("gmail.com")) // prefer gmail
        		break;
        	
        }
    	}
    	return result;
    }
		
}